<template>
    <el-dialog :title="dialogTitle" :visible.sync="isVisible">
        <el-form ref="menuForm" :model="menuForm" :rules="rules" label-width="100px">
            <el-form-item label="上级" prop="parentId">
                <el-select ref="selectReport" v-model="menuForm.parentId"
                           placeholder="顶级目录" style="width: 180px;"
                >
                    <el-option :value="menuForm.parentId" :label="menuForm.parentName"
                               style="width: 180px; height: 200px; overflow: auto; background-color: #fff;"
                    >
                        <el-tree
                            default-expand-all
                            :props="defaultProps"
                            :data="treeSelectList"
                            @node-click="handleNodeClick"
                        />
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="名称" prop="label">
                <el-input v-model="menuForm.label" />
            </el-form-item>
            <el-form-item label="图标" prop="icon">
                <el-input v-model="menuForm.icon" />
            </el-form-item>
            <el-form-item label="组件" prop="component">
                <el-input v-model="menuForm.component" />
            </el-form-item>
            <el-form-item label="路径" prop="path">
                <el-input v-model="menuForm.path" />
            </el-form-item>
            <el-form-item label="权限" prop="resourceSign">
                <el-input v-model="menuForm.resourceSign" />
            </el-form-item>
            <el-form-item label="排序" prop="sortValue">
                <el-input v-model="menuForm.sortValue" />
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-radio-group v-model="menuForm.type.code">
                    <el-radio label="MENU">菜单</el-radio>
                    <el-radio label="OPERATION">操作权限</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="显示状态" prop="hidden">
                <el-radio-group v-model="menuForm.hidden">
                    <el-radio :label=true>显示</el-radio>
                    <el-radio :label=false>隐藏</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" plain size="small" @click="submitForm('menuForm')">保存</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import menuApi from '@/api/system/menu'
export default {
    name: 'Edit',
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        type: {
            type: String,
            default: 'add'
        },
        title: {
            type: String,
            default: '添加'
        }
    },
    data() {
        return {
            // 因为不能直接修改 props 接收到的值，所以用一个变量来接收，修改这个变量
            dialogType: this.type,
            dialogTitle: this.title,
            menuForm: {
                parentId: 0,
                parentName: '顶级目录',
                label: '',
                icon: '',
                component: '',
                path: '',
                resourceSign: '',
                sortValue: 0,
                type: {code: 'MENU', desc: '菜单'},
                hidden: true
            },
            rules: {
                label: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                ],
                component: [
                    { min: 2, max: 250, message: '长度在 2 到 250 个字符', trigger: 'blur' }
                ],
                path: [
                    { min: 2, max: 250, message: '长度在 2 到 250 个字符', trigger: 'blur' }
                ],
                resourceSign: [
                    { required: true, message: '请输入地址', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ]
            },
            treeSelectList: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            }
        }
    },
    computed: {
        isVisible: {
            get() {
                return this.dialogVisible
            },
            set() {
                this.close()
            }
        }
    },
    methods: {
        getTableData(tableData) {
            this.treeSelectList = tableData.concat()
            this.treeSelectList.unshift({id: 0, label: '顶级目录'})
        },
        close() {
            this.$emit('close')
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 判断是添加还是修改
                    if (_this.dialogType == 'add') {
                        menuApi.add(this.menuForm).then(() => {
                            _this.isVisible = false
                            _this.$emit('success')
                        })
                    } else {
                        menuApi.update(this.menuForm).then(() => {
                            _this.isVisible = false
                            _this.$emit('success')
                        })
                    }
                } else {
                    return false
                }
            })
        },
        getMenu(id) {
            // 清空表单
            this.resetForm('menuForm')
            // 回显数据
            menuApi.get(id).then(res => {
                if (res.data.data.parentId == 0) {
                    res.data.data.parentName = '顶级目录'
                }
                this.menuForm = res.data.data
            })
        },
        resetForm(formName) {
            this.menuForm.parentId = 0
            this.menuForm.parentName = '顶级目录'
            this.$refs[formName].resetFields()
        },
        handleNodeClick(node) {
            this.menuForm.parentName = node.label
            this.menuForm.parentId = node.id
        }
    }
}
</script>
